package gatperdut.chato.core.interpreter.actions;

import gatperdut.chato.constants.CSConstantsCommandResult.CommandExecutionResult;
import gatperdut.chato.constants.CSConstantsString;
import gatperdut.chato.core.characters.BaseController;
import gatperdut.chato.core.interpreter.actions.generic.AdminAction;
import gatperdut.chato.core.interpreter.grammar.comminfo.MudStatsInfo;

public class MudStatsAction extends AdminAction {

    public MudStatsAction( MudStatsInfo info, BaseController entityController ) {
        super( info, entityController );
    }

    @Override
    public CommandExecutionResult perform() {
        checkForRunningCraft();

        // for ( String s : getInfo().getSpecifics() ) {
        // System.out.println( s );
        // }
        AdminCheckResult acr = checkAdminUser();
        if ( !acr.isAllowed() ) {
            getEntityController().tx( acr.getMessage() );
            return CommandExecutionResult.OK;
        }

        StringBuilder sb = new StringBuilder( "ChatoMUD stats:" + CSConstantsString.NEWLINE );

        sb.append( getEntityController().getChatoServer().getEntityHandler().getConnections().size() + " connection(s). Number of sockets used: "
                + getEntityController().getChatoServer().getEntityHandler().getSocketToController().size() + "." + CSConstantsString.NEWLINE );
        sb.append( getEntityController().getChatoServer().getEntityHandler().getLoggedPlayers().size() + " logged players." + CSConstantsString.NEWLINE );
        sb.append( getEntityController().getChatoServer().getEntityHandler().getPlayersInGame() + " characters in game." + CSConstantsString.NEWLINE );
        sb.append( getEntityController().getChatoServer().getItemHandler().getItemToController().size() + " loaded items controllers." + CSConstantsString.NEWLINE );
        sb.append( getEntityController().getChatoServer().getDatasource().getChato().getItemContainer().getItems().size() + " items in datasource."
                + CSConstantsString.NEWLINE );
        sb.append( getEntityController().getChatoServer().getEntityHandler().getNpcToController().size() + " loaded NPCs." + CSConstantsString.NEWLINE );
        sb.append( getEntityController().getChatoServer().getRoomHandler().getRoomToController().size() + " loaded rooms." + CSConstantsString.NEWLINE );
        sb.append( getEntityController().getChatoServer().getCraftHandler().getCraftToController().size() + " loaded crafts." + CSConstantsString.NEWLINE );

        getEntityController().tx( sb.toString() );
        return CommandExecutionResult.OK;
    }

    @Override
    public MudStatsInfo getInfo() {
        return (MudStatsInfo) super.getInfo();
    }
}
